package com.partner.frame.support.util;

import java.io.*;
import java.sql.*;
import javax.servlet.http.*;

import net.sf.json.*;
import net.sf.json.util.*;
import net.sf.json.processors.*;

public class JsonUtil
{
   // json输出配置
   public static JsonConfig configJson(String datePattern)
   {
      JsonConfig jsoncfgOutput = new JsonConfig();

      jsoncfgOutput = new JsonConfig();
      jsoncfgOutput.setIgnoreDefaultExcludes(false);
      jsoncfgOutput.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);

      // 缺省值处理
      jsoncfgOutput.registerDefaultValueProcessor(Integer.class, new DefaultValueProcessor()
      {
         public Object getDefaultValue(Class classType)
         {
            return JSONNull.getInstance();
         }
      });
      jsoncfgOutput.registerJsonValueProcessor(java.sql.Date.class, new JsonDateValueProcessor());
      jsoncfgOutput.registerJsonValueProcessor(java.sql.Timestamp.class, new JsonDateValueProcessor());
      jsoncfgOutput.registerJsonValueProcessor(java.util.Date.class, new JsonDateValueProcessor());

      return jsoncfgOutput;
   }

   public class CysysIntegerDefaultIntegerValueProcessor implements DefaultValueProcessor
   {

      public Object getDefaultValue(Class classType)
      {
         String strValue = "";
         
         if(classType != null && Integer.class.isAssignableFrom(classType))
         {
            strValue = classType.toString();
            return strValue;   
         }
         
         return JSONNull.getInstance();
      }
   }
   
   public static void outJson(Object objData, HttpServletResponse httpResponse)
   {
      JsonConfig jsonConfig = configJson("yyyy-MM-dd HH:mm:ss");
      
      httpResponse.setContentType("application/json;charset=UTF-8");
      outString(JSONObject.fromObject(objData, jsonConfig).toString(), httpResponse);
   }
   
   public static void outJavascriptJson(Object objData, HttpServletResponse httpResponse)
   {
      JsonConfig jsonConfig = configJson("yyyy-MM-dd HH:mm:ss");

      httpResponse.setContentType("application/javascript;charset=UTF-8");
      outString(JSONObject.fromObject(objData, jsonConfig).toString(), httpResponse);
   }

   public static void outHtmlJson(Object objData, HttpServletResponse httpResponse)
   {
      JsonConfig jsonConfig = configJson("yyyy-MM-dd HH:mm:ss");

      httpResponse.setContentType("text/html;charset=UTF-8");
      outString(JSONObject.fromObject(objData, jsonConfig).toString(), httpResponse);
   }
   
   private static void outString(String strJson, HttpServletResponse httpResponse)
   {
      PrintWriter pwOutput = null;

      try
      {
         // 设置页面不缓存
         httpResponse.setHeader("Pragma", "No-cache");
         httpResponse.setHeader("Cache-Control", "no-cache");
         httpResponse.addHeader("Access-Control-Allow-Origin","*");
         httpResponse.setCharacterEncoding("UTF-8");
         
         pwOutput = httpResponse.getWriter();

         //替换null和"null"
         strJson = strJson.replaceAll("\"null\"", "");
         strJson = strJson.replaceAll("null", "\"\"");
         pwOutput.write(strJson);

         //System.out.println(strJson);
      }
      catch(IOException eOutput)
      {
         eOutput.printStackTrace();
      }

      return;
   }
   
}
